﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.LoginServiceRef;

    public class UserPreference : BasePage
    {
        protected HtmlImage Black;
        protected Button btnSave;
        protected Button btnSetDefault;
        protected HtmlImage ClassicBlue;
        protected DropDownList ddlCountry;
        protected DropDownList ddlCurrency;
        protected DropDownList ddlDate_Format;
        protected DropDownList ddlDigits_No;
        protected DropDownList ddlLang;
        protected DropDownList ddlTime;
        protected DropDownList ddlTime_Format;
        protected DropDownList ddlTimeZone;
        protected HtmlForm F1050;
        protected HtmlImage FashionBlue;
        protected HtmlImage Green;
        protected HtmlInputHidden hdnLang;
        protected HtmlInputHidden hdnStyle;
        protected Label lblCountry;
        protected Label lblCurrency;
        protected Label lblDateFormat;
        protected Label lblDigitsNo;
        protected Label lblLang;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblTime;
        protected Label lblTimeFormat;
        protected Label lblTimeZone;
        protected HtmlImage LightBlue;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected HtmlImage Orange;
        protected HtmlImage Purple;
        protected RadioButton rdobtnBlack;
        protected RadioButton rdobtnClassicBlue;
        protected RadioButton rdobtnFashionBlue;
        protected RadioButton rdobtnGreen;
        protected RadioButton rdobtnLightBlue;
        protected RadioButton rdobtnOrange;
        protected RadioButton rdobtnPurple;

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PreferenceProfileInfo preferenceProfileInfo = new PreferenceProfileInfo();
            preferenceProfileInfo.PreferenceProfileID = base.UserInfo.UserID;
            preferenceProfileInfo.SystemCreated = "N";
            preferenceProfileInfo.UserID = preferenceProfileInfo.PreferenceProfileID;
            preferenceProfileInfo.CreatedBy = preferenceProfileInfo.UserID;
            preferenceProfileInfo.UpdatedBy = preferenceProfileInfo.CreatedBy;
            preferenceProfileInfo.IsReset = false;
            preferenceProfileInfo.PreferenceProfileDetailInfos = new List<PreferenceProfileDetailInfo>();
            string strchangeStyle = string.Empty;
            string strlanguage = string.Empty;
            foreach (Control c in base.Form.Controls)
            {
                PreferenceProfileDetailInfo prdferenceProfileDetailInfo;
                if (c is DropDownList)
                {
                    DropDownList ddl = c as DropDownList;
                    if (ddl.ID == "ddlLang")
                    {
                        strlanguage = ddl.SelectedValue;
                    }
                    prdferenceProfileDetailInfo = new PreferenceProfileDetailInfo();
                    prdferenceProfileDetailInfo.PreferenceProfileID = preferenceProfileInfo.PreferenceProfileID;
                    prdferenceProfileDetailInfo.PreferenceID = ddl.ID.Replace("ddl", "").ToUpper();
                    prdferenceProfileDetailInfo.PreferenceValue = ddl.SelectedValue;
                    prdferenceProfileDetailInfo.CreatedBy = preferenceProfileInfo.CreatedBy;
                    prdferenceProfileDetailInfo.UpdatedBy = prdferenceProfileDetailInfo.CreatedBy;
                    preferenceProfileInfo.PreferenceProfileDetailInfos.Add(prdferenceProfileDetailInfo);
                }
                else if (c is RadioButton)
                {
                    RadioButton rdobtn = c as RadioButton;
                    if (rdobtn.Checked)
                    {
                        prdferenceProfileDetailInfo = new PreferenceProfileDetailInfo();
                        prdferenceProfileDetailInfo.PreferenceProfileID = preferenceProfileInfo.PreferenceProfileID;
                        prdferenceProfileDetailInfo.PreferenceID = rdobtn.GroupName;
                        prdferenceProfileDetailInfo.PreferenceValue = rdobtn.ID.Replace("rdobtn", "");
                        prdferenceProfileDetailInfo.CreatedBy = preferenceProfileInfo.CreatedBy;
                        prdferenceProfileDetailInfo.UpdatedBy = prdferenceProfileDetailInfo.CreatedBy;
                        preferenceProfileInfo.PreferenceProfileDetailInfos.Add(prdferenceProfileDetailInfo);
                        strchangeStyle = prdferenceProfileDetailInfo.PreferenceValue;
                    }
                }
            }
            try
            {
                this.lblMsg.Text = LoginServiceClientProxy.GetInstance().InsertUserPreference(preferenceProfileInfo) + base.GetGlobalMsgResource("F1500_001");
                this.SetSessionData();
                this.hdnLang.Value = base.ChangeLangeugePattern(base.UserInfo.Preference.Lang);
                this.hdnStyle.Value = base.ChangeStylePattern(base.UserInfo.Preference.Themes);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> RefreshMenu();</script>", false);
        }

        protected void btnSetDefault_Click(object sender, EventArgs e)
        {
            PreferenceProfileInfo preferenceProfileInfo = new PreferenceProfileInfo {
                PreferenceProfileID = base.UserInfo.UserID,
                SystemCreated = "N",
                IsReset = true
            };
            try
            {
                this.lblMsg.Text = LoginServiceClientProxy.GetInstance().InsertUserPreference(preferenceProfileInfo) + base.GetGlobalMsgResource("F1500_002");
                this.SetSessionData();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> RefreshMenu();</script>", false);
        }

        private void LoadData()
        {
            base.BindData2DDLByPreValue(this.ddlCountry, "COUNTRY");
            base.BindData2DDLByPreValue(this.ddlCurrency, "CURRENCY");
            base.BindData2DDLByPreValue(this.ddlDate_Format, "DATE_FORMAT");
            base.BindData2DDLByPreValue(this.ddlDigits_No, "DIGITS_NO");
            base.BindData2DDLByPreValue(this.ddlLang, "LANG");
            base.BindData2DDLByPreValue(this.ddlTime_Format, "TIME_FORMAT");
            base.BindData2DDLByPreValue(this.ddlTimeZone, "TIMEZONE");
            base.BindData2DDLByPreValue(this.ddlTime, "TIME");
            if (base.UserInfo.Preference != null)
            {
                PreferenceInfo preferenceInfo = base.UserInfo.Preference;
                this.ddlCountry.SelectedValue = preferenceInfo.Country;
                this.ddlCurrency.SelectedValue = preferenceInfo.Currency;
                this.ddlDate_Format.SelectedValue = preferenceInfo.DateFormat;
                this.ddlDigits_No.SelectedValue = preferenceInfo.Digits_No;
                this.ddlLang.SelectedValue = preferenceInfo.Lang;
                this.ddlTime_Format.SelectedValue = preferenceInfo.Time_Format;
                this.ddlTimeZone.SelectedValue = preferenceInfo.TimeZone;
                this.ddlTime.SelectedValue = preferenceInfo.Time;
                switch (preferenceInfo.Themes)
                {
                    case "Orange":
                        this.rdobtnOrange.Checked = true;
                        break;

                    case "ClassicBlue":
                        this.rdobtnClassicBlue.Checked = true;
                        break;

                    case "Black":
                        this.rdobtnBlack.Checked = true;
                        break;

                    case "Green":
                        this.rdobtnGreen.Checked = true;
                        break;

                    case "LightBlue":
                        this.rdobtnLightBlue.Checked = true;
                        break;

                    case "Purple":
                        this.rdobtnPurple.Checked = true;
                        break;

                    case "FashionBlue":
                        this.rdobtnFashionBlue.Checked = true;
                        break;
                }
            }
            if ((base.UserInfo.UserRoleInfos != null) && (base.UserInfo.UserRoleInfos.Count > 0))
            {
                for (int i = 0; i < base.UserInfo.UserRoleInfos.Count; i++)
                {
                    if (base.UserInfo.UserRoleInfos[i].RoleID.ToUpper() == "ADMINISTRATOR")
                    {
                        this.ddlDigits_No.Enabled = true;
                        break;
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSessionData()
        {
            try
            {
                PreferenceProfileInfo preferencePrifileInfo = LoginServiceClientProxy.GetInstance().GetPreferenceProfileByUserID(base.UserInfo.UserID);
                base.UserInfo.Preference = new PreferenceInfo();
                for (int i = 0; preferencePrifileInfo.PreferenceProfileDetailInfos.Count > i; i++)
                {
                    switch (preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceID.ToUpper())
                    {
                        case "COUNTRY":
                            base.UserInfo.Preference.Country = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "CURRENCY":
                            base.UserInfo.Preference.Currency = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "DATE_FORMAT":
                            base.UserInfo.Preference.DateFormat = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "DIGITS_NO":
                            base.UserInfo.Preference.Digits_No = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "LANG":
                            base.UserInfo.Preference.Lang = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            this.hdnLang.Value = base.ChangeLangeugePattern(base.UserInfo.Preference.Lang);
                            break;

                        case "TIME_FORMAT":
                            base.UserInfo.Preference.Time_Format = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "TIMEZONE":
                            base.UserInfo.Preference.TimeZone = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;

                        case "THEMES":
                            base.UserInfo.Preference.Themes = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            this.hdnStyle.Value = base.ChangeStylePattern(base.UserInfo.Preference.Themes);
                            break;

                        case "TIME":
                            base.UserInfo.Preference.Time = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

